package com.xxcxcxcx.jobsystem.mixin.job.Cook;


import com.xxcxcxcx.jobsystem.Main;
import com.xxcxcxcx.jobsystem.common.Job.Cook.Cook;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.food.FoodProperties;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.extensions.IForgeItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

import javax.annotation.Nullable;

import static com.xxcxcxcx.jobsystem.common.Job.Cook.Cook.*;

@Mixin(ItemStack.class)
public abstract class ItemStackMixin implements IForgeItemStack {
    @Shadow public abstract CompoundTag getOrCreateTagElement(String p_41699_);

    @Shadow public abstract Item getItem();

    @Shadow public abstract void removeTagKey(String p_41750_);

    @Override
    @Nullable
    public FoodProperties getFoodProperties(@Nullable LivingEntity entity) {
        FoodProperties oldfoodProperties = this.getItem().getFoodProperties();
        if(oldfoodProperties==null)return null;
        CompoundTag nbt = this.getOrCreateTagElement(Main.MOD_ID);
        if(nbt.contains(MakeFood)){
            if(nbt.getString(MakeFood+FoodLevel).equals(UnIdentification)){
                return null;
            }
            if(nbt.getString(MakeFood+FoodLevel).equals("")){
                FoodProperties.Builder builder = (new FoodProperties.Builder());
                if(nbt.contains(MakeFood+ Cook.Newnutrition)){
                    builder.nutrition(oldfoodProperties.getNutrition()/2);
                }
                if(nbt.contains(MakeFood+Cook.Newsaturation)){
                    builder.saturationMod(oldfoodProperties.getSaturationModifier()/2);
                }
                oldfoodProperties.getEffects().forEach((a)->builder.effect(a.getFirst(),a.getSecond()));
                builder.effect(new MobEffectInstance(MobEffects.CONFUSION,600,0),0.3f);
                builder.effect(new MobEffectInstance(MobEffects.BLINDNESS,600,0),0.3f);
                builder.effect(new MobEffectInstance(MobEffects.POISON,200,0),0.3f);
                builder.effect(new MobEffectInstance(MobEffects.HUNGER,200,0),0.3f);
                return builder.build();
            }
            FoodProperties.Builder builder = (new FoodProperties.Builder());
            if(nbt.contains(MakeFood+Newnutrition)){
                builder.nutrition(oldfoodProperties.getNutrition()+
                        nbt.getInt(MakeFood+ Cook.Newnutrition));
            }
            if(nbt.contains(MakeFood+Newsaturation)){
                builder.saturationMod(oldfoodProperties.getSaturationModifier()+
                        nbt.getFloat(MakeFood+Cook.Newsaturation));
            }
            if(nbt.contains(MakeFood+NewisMeat)|| oldfoodProperties.isMeat()){
                builder.meat();
            }
            if(nbt.contains(MakeFood+NewcanAlwaysEat)|| oldfoodProperties.canAlwaysEat()){
                builder.alwaysEat();
            }
            if(nbt.contains(MakeFood+NewfastFood)|| oldfoodProperties.isFastFood()){
                builder.fast();
            }
            oldfoodProperties.getEffects().forEach((a)->builder.effect(a.getFirst(),a.getSecond()));
            for(int i=1;i<=32;i++){
                if(nbt.contains(MakeFood+Effect+ i)){
                    int time=nbt.getInt(MakeFood+Effect+ i +EffectTime);
                    int level=nbt.getInt(MakeFood+Effect+ i +EffectLevel);
                    float chance=nbt.getFloat(MakeFood+Effect+ i +EffectChance);
                    MobEffect mobEffect = MobEffect.byId(i);
                    builder.effect(new MobEffectInstance(mobEffect,time,level),chance);
                }
            }
            return builder.build();
        }else{
            this.removeTagKey(Main.MOD_ID);
            FoodProperties.Builder builder = (new FoodProperties.Builder());
            builder.nutrition(oldfoodProperties.getNutrition()/2);
            builder.saturationMod(oldfoodProperties.getSaturationModifier()/2);
            oldfoodProperties.getEffects().forEach((a)->builder.effect(a.getFirst(),a.getSecond()));
            builder.effect(new MobEffectInstance(MobEffects.CONFUSION,600,0),0.3f);
            builder.effect(new MobEffectInstance(MobEffects.BLINDNESS,600,0),0.3f);
            builder.effect(new MobEffectInstance(MobEffects.POISON,200,0),0.05f);
            builder.effect(new MobEffectInstance(MobEffects.HUNGER,200,0),0.05f);
            if(oldfoodProperties.isMeat()){
                builder.meat();
            }
            if(oldfoodProperties.canAlwaysEat()){
                builder.alwaysEat();
            }
            if(oldfoodProperties.isFastFood()){
                builder.fast();
            }
            return builder.build();
        }
    }
}
